<template>
  <div>
    <page-tools :show-before="true">
      <span slot="before">共{{ total }}条记录</span>
      <template slot="after">
        <el-button size="small" type="warning" @click="$router.push('/import')"
          >excel导入</el-button
        >
        <el-button size="small" type="danger" @click="exportData">
          excel导出</el-button
        >
        <!-- <el-button size="small" type="danger">excel导入</el-button> -->
        <el-button
          size="small"
          type="primary"
          @click="dialogAddEmpVisible = true"
          >新增员工</el-button
        >
      </template>
    </page-tools>

    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="username" label="姓名" width="100">
      </el-table-column>
      <el-table-column prop="staffPhoto" label="图像" width="150">
        <template slot-scope="{ row }">
          <img
            v-if="row.staffPhoto"
            style="width:100px;height:100px;border-radius:50%;"
            :src="row.staffPhoto"
            alt=""
            v-imageError2="defaultImg"
            @click="showQrcode(row.staffPhoto)"
          />
          <img
            v-if="row.staffPhoto == null"
            style="width:100px;height:100px;border-radius:50%;"
            alt=""
            :src="defaultImg"
            @click="showQrcode(defaultImg)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="手机" width="120">
      </el-table-column>
      <el-table-column prop="workNumber" label="工号" width="180">
      </el-table-column>
      <el-table-column label="聘用形式" width="120">
        <template slot-scope="{ row }">
          {{ row.formOfEmployment == 1 ? "正式" : "非正式" }}
        </template>
      </el-table-column>
      <el-table-column prop="departmentName" label="部门" width="120">
      </el-table-column>
      <el-table-column prop="timeOfEntry" label="入职时间" width="120">
        <template slot-scope="{ row }">
          {{ row.timeOfEntry | timeObj("YYYY-MM-DD") }}
        </template>
      </el-table-column>
      <el-table-column prop="enableState" label="账户状态" width="120">
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="250">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            size="mini"
            @click="$router.push('/detail/' + row.id)"
            >查看</el-button
          >
          <el-button type="text" size="mini">转正</el-button>
          <el-button type="text" size="mini">调岗</el-button>
          <el-button type="text" size="mini">离职</el-button>
          <el-button type="text" size="mini" @click="roleHandle(row.id)"
            >角色</el-button
          >
          <el-button type="text" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <!-- 分页 -->

    <el-pagination
      style="text-align:right;margin:20px  0 "
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="paramsObj.page"
      :page-size="paramsObj.size"
      layout="total, prev, pager, next"
      :total="total"
    >
    </el-pagination>

    <!-- 添加员工对话框 -->
    <add-emp
      @refresh="refresh"
      ref="tree"
      :dialogAddEmpVisible="dialogAddEmpVisible"
    ></add-emp>

    <!-- 显示二维码对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-row type="flex" justify="center">
        <canvas ref="myCanvas" />
      </el-row>
    </el-dialog>

    <!-- 角色 -->
    <!-- <el-dialog title="分配角色" :visible.sync="dialogVisibleRole" width="60%">
      ==={{ roleIds }}
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{
          item.name
        }}</el-checkbox>
      </el-checkbox-group>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleRole = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisibleRole = false"
          >确 定</el-button
        >
      </span>
    </el-dialog> -->
    <assign-role
      :dialogVisibleRole.sync="dialogVisibleRole"
      ref="roles"
      :userId="userId"
    ></assign-role>
  </div>
</template>

<script>
import { getEmployees } from "@/api/employee";

import QrCode from "qrcode";

import dayjs from "dayjs";
// import pageTools from "@/components/common/pageTools";

export default {
  // components: {
  //   pageTools
  // },
  name: "",
  data() {
    return {
      paramsObj: {
        page: 1,
        size: 5
      },
      total: 9,
      tableData: [],
      defaultImg: require("@/assets/common/head.jpg"),

      /* 添加员工 */
      dialogAddEmpVisible: false,
      dialogVisible: false,
      /* 角色分配 */
      dialogVisibleRole: false,
      userId: null
      // roleIds: [], //  选中的角色
      // roleList: [] //  所有的角色
    };
  },

  created() {
    //console.log(this, 1111);
    this.getEmployeesList();
  },
  methods: {
    /* 根据id获取角色列表 */

    /* 获取角色列表 */
    async roleHandle(id) {
      /* 
      显示对话框
      显示角色列表
      显示id对应的角色
       */
      this.userId = id;

      this.$refs.roles.getRolesById(id);
      //console.log(this.$refs.roles, 8888);
      this.dialogVisibleRole = true;
    },
    async getEmployeesList() {
      const { total, rows } = await getEmployees(this.paramsObj);
      // console.log(res, 111);
      this.tableData = rows;
      this.total = total;
    },

    /* 分页 */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`);
      this.paramsObj.page = val; //  重置当前页
      this.getEmployeesList();
    },
    refresh() {
      this.getEmployeesList();
      this.dialogAddEmpVisible = false;
    },
    /* 导出表格文件 */
    exportData() {
      // 表头对应关系
      const headers = {
        姓名: "username",
        手机号: "mobile",
        入职日期: "timeOfEntry",
        聘用形式: "formOfEmployment",
        转正日期: "correctionTime",
        工号: "workNumber",
        部门: "departmentName"
      };
      import("@/vendor/Export2Excel").then(async excel => {
        /* 获取所有的名单数据 */
        const { rows } = await getEmployees({
          page: 1,
          size: this.total
        });

        const data = this.formatJson(headers, rows);

        /* 配置表头复杂格式 */
        const multiHeader = [["姓名", "主要信息", "", "", "", "", "部门"]];
        const merges = ["A1:A2", "B1:F1", "G1:G2"];

        //console.log(rows, 888);
        excel.export_json_to_excel({
          multiHeader,
          merges,
          header: Object.keys(headers), //表头 必填
          data, //具体数据 必填
          filename: "花名册", //非必填
          autoWidth: true, //非必填
          bookType: "xlsx" //非必填
        });
      });
    },
    //  处理tbody中的数据  和表头一直
    formatJson(headers, rows) {
      return rows.map(item => {
        return Object.keys(headers).map(key => {
          if (
            headers[key] == "timeOfEntry" ||
            headers[key] == "correctionTime"
          ) {
            //return timeObj(item[headers[key]],"YYYY-MM-DD")
            return dayjs(item[headers[key]]).format("YYYY-MM-DD");
          }
          // Object.keys(headers)  ['姓名','手机号'。。。]    key   ：姓名   手机号
          //  headers[key]    =>    username  mobile
          return item[headers[key]]; //  [‘管理员’，’1380000002‘,....]
        });
      });
      //  最后返回出来的是：[[‘管理员’，’1380000002‘,....],[‘管理员’，’1380000002‘,....]
    },

    /* code */
    showQrcode(url) {
      console.log(url, 999);
      this.dialogVisible = true;

      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.myCanvas, url);
      });
    }
  }
};
</script>
<style lang="less" scoped></style>
